using System;
using System.Threading;

internal sealed class AClass {
   private static void CallbackWithoutNewingADelegateObject() {
      ThreadPool.QueueUserWorkItem(
         delegate(Object obj) { Console.WriteLine(obj); }, 5);
   }
}

internal sealed class AClass2 {
   private static void UsingLocalVariablesInTheCallbackCode(Int32 numToDo) {

      // Some local variables
      Int32[] squares = new Int32[numToDo];
      AutoResetEvent done = new AutoResetEvent(false);

      // Do a bunch of tasks on other threads
      for (Int32 n = 0; n < squares.Length; n++) {
         ThreadPool.QueueUserWorkItem(
            delegate(Object obj)
            {
               Int32 num = (Int32) obj;

               // This task would normally more time consuming
               squares[num] = num * num;

               // If last task, let main thread continue running
               if (Interlocked.Decrement(ref numToDo) == 0)
                  done.Set();
            }, n);
      }

      // Wait for all the other threads to finish
      done.WaitOne();

      // Show the results
      for (Int32 n = 0; n < squares.Length; n++)
         Console.WriteLine("Index {0}, Square={1}", n, squares[n]);
   }
}


internal static class Program {
   static void Main() {
      // Create and initialize a String array
      String[] names = { "Jeff", "Kristin", "Aidan" };

      // Get just the names that have a lowercase 'i' in them.
      Char charToFind = 'i';
      names = Array.FindAll(names, delegate(String name) 
         { return (name.IndexOf(charToFind) >= 0); });

      // Convert each string's characters to uppercase
      names = Array.ConvertAll<String, String>(names, delegate(String name) 
         { return name.ToUpper(); });

      // Sort the names
      Array.Sort(names, delegate(String name1, String name2) 
         { return String.Compare(name1, name2); });

      // Display the results
      Array.ForEach(names, delegate(String name) { Console.WriteLine(name); });
   }
}